package managers;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import entities.Event;
import entities.Message;
import entities.User;

@Stateless
public class EntityFunctionManager {
	
	@PersistenceContext
	private EntityManager m;
	
	/**
	 * La fonction pour retrouver les nouveaux utilisateurs, il est utilise par l'administrateur.
	 * @param 
	 * @return List<User>
	 */
	public List<User> listUsersNonValide() {
		TypedQuery<User> request = m.createQuery("select c from User c where validated=0", User.class);		
		return request.getResultList();
	}
	
	/**
	 * L'inverse de la fonction listUsersNonValide, il est utilise lorsque
	 * on veut banner les utilisateurs.
	 * @return
	 */
	public List<User> listUsersValide() {
		TypedQuery<User> request = m.createQuery("select c from User c where validated=1", User.class);
		
		return request.getResultList();
	}
	
	
	/**
	 * Get all users
	 * @return
	 */
	public List<User> listUsers() {
		TypedQuery<User> request = m.createQuery("select c from User c", User.class);
		return request.getResultList();
	}
	
	
	/**
	 * Chercher l'utilisateur par son nom
	 * @param name
	 * @return
	 */
	public User searchByName(String name) {
		System.out.println("select c from User c where user='"+name+"'");
		TypedQuery<User> request = m.createQuery("select c from User c where user='"+name+"'",User.class);
		return request.getSingleResult();
	}
	
	/**
	 * Recuperer la liste de tous les messages
	 * @param user
	 * @return
	 */
	public List<Message> listMessages(String user) {
		TypedQuery<Message> request= m.createQuery("select m from Message m where user_user= :us ",Message.class);
		request.setParameter("us", user);
		return request.getResultList();
	}
	
	/**
	 * On cherche les eventments selon les promoteurs
	 * @param user
	 * @return
	 */
	public List<Event> searchEventByPromoter(String user) {
		TypedQuery<Event> request = (TypedQuery<Event>) m.createQuery("select e from Event e where Promoter_id = :us and open=1");
		request.setParameter("us", user);
		return request.getResultList();
	}
	
	/**
	 * On cherche les eventments pour selon la place.
	 * @param place
	 * @return
	 */
	public List<Event> searchEventByPlace(String place) {
		TypedQuery<Event> request = (TypedQuery<Event>) m.createQuery("select e from Event e where place = :us and open=1");
		request.setParameter("us", place);
		return request.getResultList();
	}
	
	/**
	 * On cherche les eventments par l'heure.
	 * @param hour
	 * @return
	 */
	public List<Event> searchEventByHour(String hour) {
		System.out.println("Not error");
		TypedQuery<Event> request = (TypedQuery<Event>) m.createQuery("select e from Event e where hour = '"+hour +"' and open=1");
		//request.setParameter("us", hour);
		return request.getResultList();
	}
	
	/**
	 * Recuperes les eventments selon la date
	 * @param hour
	 * @return
	 */
	public List<Event> searchEventByDate(String hour) {
		System.out.println("Not error");
		TypedQuery<Event> request = (TypedQuery<Event>) m.createQuery("select e from Event e where date = '"+hour +"' and open=1");
		//request.setParameter("us", hour);
		return request.getResultList();
	}
	
	/**
	 * Recuperer les eventments selon ses prenoms.
	 * @param place
	 * @return
	 */
	public List<Event> searchEventByName(String place) {
		TypedQuery<Event> request = (TypedQuery<Event>) m.createQuery("select e from Event e where name = :us and open=1");
		request.setParameter("us", place);
		return request.getResultList();
	}
	
	/**
	 * Methode utilise lorsque l'administrateur veut valider les eventments.
	 * @return
	 */
	public List<Event> listNewEvents() {
		TypedQuery<Event> request = (TypedQuery<Event>) m.createQuery("select e from Event e where open=0");
		return request.getResultList();
	}
	
}
